package com.arcsoft.api.system.face;

import com.arcsoft.face.EngineConfiguration;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;

class FaceEngineFactory implements PooledObjectFactory<AFaceEngine> {
    private boolean coreObjectCreated = false;
    private EngineConfiguration engineConfiguration;
    private EngineConfiguration coreEngineConfiguration;
    private String appId;
    private String sdkKey;
    private String activeKey;

    public FaceEngineFactory(EngineConfiguration engineConfiguration, EngineConfiguration coreEngineConfiguration, String appId, String sdkKey, String activeKey){
        super();
        this.engineConfiguration = engineConfiguration;
        this.coreEngineConfiguration = coreEngineConfiguration;
        this.appId = appId;
        this.sdkKey = sdkKey;
        this.activeKey = activeKey;
    }

    @Override
    public PooledObject<AFaceEngine> makeObject() {
        AFaceEngine faceEngine = new AFaceEngine(true,"/usr/local/arcsoft_lib");  // 创建核心对象
        faceEngine.activeOnline(appId,sdkKey,activeKey);
        faceEngine.setLivenessParam(0.5f, 0.7f);
        if (!coreObjectCreated) {
            //因为核心引擎仅做注册与识别，为了节省资源，故此只初始化最近本识别检测功能
            faceEngine.init(coreEngineConfiguration);
            coreObjectCreated = true;
            System.out.println("Created core FaceEngine.");
        } else {
            //非核心引擎要做人脸各项功能，故单独初始化所有功能
            faceEngine.init(engineConfiguration);
            System.out.println("Created regular FaceEngine.");
        }
        return new DefaultPooledObject<>(faceEngine);
    }

    @Override
    public void destroyObject(PooledObject<AFaceEngine> p) {
        p.getObject().unInit();
    }

    @Override
    public boolean validateObject(PooledObject<AFaceEngine> p) {
        return true;
    }

    @Override
    public void activateObject(PooledObject<AFaceEngine> p) {
        System.out.println("Activating FaceEngine.");
    }

    @Override
    public void passivateObject(PooledObject<AFaceEngine> p) {
        System.out.println("Passivating FaceEngine.");
    }
}
